import React, { useReducer, createContext } from 'react'
import Com from './Com'
import Son from './Son'

let context = createContext()

export {
    context
}

let reducer = (state, action) => {
    let NewState =  JSON.parse(JSON.stringify(state))
    switch(action.type) {
        case "CHANGE_NAME":
            NewState.name = "李四"
            return NewState
    }
}

let initState = {
    name: "张三",
    age: 20
}

function Providers() {
    const [state, dispatch] = useReducer(reducer, initState)
    return (
        <div>
            <context.Provider value = {{
                state,
                dispatch
            }}>
                <Com/>
                <Son/>
            </context.Provider>
        </div>
    )
}

export default Providers
